package binary_tree.LeetCode;


import java.util.Deque;
import java.util.LinkedList;

/**
 * 使用层序遍历判断二叉树是否为镜像对称
 */
public class Num101_levelOrderTraversal {
    public boolean isSymmetric(TreeNode root) {
        if (root==null){
            return true;
        }
        Deque<TreeNode> deque=new LinkedList<>();
        //先判断根节点左右子树节点
        deque.offer(root.left);
        deque.offer(root.right);
        while (!deque.isEmpty()){
            TreeNode r1=deque.poll();
            TreeNode r2=deque.poll();
            if (r1==null&r2==null){
                continue;
            }
            if (r1==null||r2==null){
                return false;
            }
            if (r1.val!=r2.val){
                return false;
            }
        //让r1.left和r2.right入队
        deque.offer(r1.left);
        deque.offer(r2.right);
        //让r1.right和r2.left入队
        deque.offer(r1.right);
        deque.offer(r2.left);
        }
        return true;
    }

}
